/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main.genomemap;

import genomemap.barratt.*;

import org.stat.model.data.Record;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 *
 * @version version
 * @author Susanta Tewari
 * @history Created on 12/23/12
 * @since since
 */
public class FitBarrattModel {

    /**
     * the best value of k is 1.0
     * @param args arguments
     */
    public static void main(String[] args) {

        final String barratt_file_path          = "resources/barratt_model_data.txt";
        final BarrattModelDAO barrattModelDAO   = new BarrattModelFileDAO(barratt_file_path);
        final BarrattModelData barrattModelData = barrattModelDAO.getBarrattModelData();
        BarrattInterferenceModel barrattModel   = new BarrattInterferenceModel(barrattModelData);


        // search for k.
        SortedMap mseMap = new TreeMap();
        BarrattModelEstimator barrattModelEstimator;

        for (int i = 0; i < 11; i++) {

            double k = 0.1 * (i);

            barrattModelEstimator = new BarrattModelEstimator(k);

            barrattModel.setModelEstimator(barrattModelEstimator);

            double mse = barrattModel.getMSE();

            mseMap.put(new Double(mse), new Double(k));
        }

        System.out.println("MSE comparison: ");
        System.out.println(mseMap);

        double k = ((Double) mseMap.get(mseMap.firstKey())).doubleValue();

        barrattModelEstimator = new BarrattModelEstimator(k);

        barrattModel.setModelEstimator(barrattModelEstimator);
        barrattModel.runModel();
        System.out.println("Best Mapping function: k :" + k);
        System.out.println("observed estimated observedX ");

        Record[] records = barrattModel.getModelData().getRecords();

        for (Record record : records) {

            double observed  = record.getData()[0].getValue();
            double observedX = record.getData()[1].getValue();
            double estimated = record.getData()[0].getEstimatedValue();

            System.out.println(observed + "  " + estimated + "  " + observedX);
        }
    }
}
